/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.misc.sap.jco.config;

import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.ext.DestinationDataProvider;
import com.sap.conn.jco.ext.Environment;
import org.vacoor.nothing.common.util.Resources;
import org.vacoor.nothing.misc.sap.jco.binding.Session;

import java.io.File;
import java.util.Properties;

/**
 * Jco 配置 ,  入口
 * <p/>
 * 配置文件说明见: {@link ClassPathConfigDestinationDataProvider}
 *
 * @author vacoor
 */
public class JcoConfig {
    private static final PropertiesConfigDestinatioinDataProvider PCDD = new PropertiesConfigDestinatioinDataProvider();
    private static final ClassPathConfigDestinationDataProvider CPCDD = new ClassPathConfigDestinationDataProvider();

    public static JcoConfig configure(String destinationName, String configFile) {
        return configure(destinationName, Resources.getResourceAsProperties(configFile));
    }

    public static JcoConfig configure(String destinationName, Properties props) {
        PCDD.addDestination(destinationName, props);
        registerDestinationDataProvider(PCDD);
        return newInstance();
    }

    /**
     * 使用 {@link ClassPathConfigDestinationDataProvider} 来提供配置
     *
     * @return
     */
    public static JcoConfig configureUseClassPathFile() {
        registerDestinationDataProvider(CPCDD);
        return newInstance();
    }

    /**
     * 使用 JCO 默认的配置加载方式(从 user.dir 下查找 destinationName.jcoDestination)
     *
     * @return
     */
    public static JcoConfig configureUseJcoDefault() {
        return newInstance();
    }

    private static JcoConfig newInstance() {
        return new JcoConfig();
    }

    /**
     * JCO 会自动在 java.library.path 查找 native lib, (自己手动加载会报错, 不过可以忽略)
     * String property = System.getProperty("java.library.path");
     * System.setProperty("java.library.path", property + File.pathSeparatorChar + "/home/vacoor/.sap/");
     */
    public static void addNativeLibDir(String libDir) {
        /*
        File file = new File(libDir);
        if (!file.exists() || !file.isDirectory()) {
            throw new RuntimeException("无效的目录:" + libDir);
        }
        String path = file.getCanonicalPath();
        */
        String path = libDir;
        String property = System.getProperty("java.library.path");
        System.setProperty("java.library.path", property + File.pathSeparatorChar + path);

    }

    private static void registerDestinationDataProvider(DestinationDataProvider destinationDataProvider) {
        // TODO 这里不知道会不会覆盖
        Environment.registerDestinationDataProvider(destinationDataProvider);
    }

    /**
     * ***************************  实例方法   ***************************
     */
    private JcoConfig() {
    }

    public Session openDestination(String destinationName) throws JCoException {
        return openDestination(destinationName, null);
    }

    public Session openDestination(String destinationName, String scopeType) throws JCoException {
        JCoDestination destination = JCoDestinationManager.getDestination(destinationName, scopeType);
        return new Session(destination);
    }
}
